package com.javaee.web.servlet;
import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Activity;
import com.javaee.ejb.entity.Conversation;
import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.entity.Project;
import com.javaee.ejb.session.remote.ActivityManagerRemote;
import com.javaee.ejb.session.remote.ConversationManagerRemote;
import com.javaee.ejb.session.remote.EmployeeManagerRemote;
import com.javaee.ejb.session.remote.ProjectManagerRemote;
import com.javaee.web.HttpServletBase;
import com.javaee.web.SiteParameters;

/**
 * Servlet implementation class ProjectServlet
 */
public class ProjectServlet extends HttpServletBase {
	private static final long serialVersionUID = 1L;

	@EJB
	private EmployeeManagerRemote employeeManager;
	@EJB
	private ActivityManagerRemote activityManager;
	@EJB
	private ConversationManagerRemote conversationManager;
	@EJB
	private ProjectManagerRemote projectManager;

	/**
	 * @see HttpServletBase#HttpServletBase()
	 */
	public ProjectServlet() {
		super();
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// On recupere l'id du projet
		int projectId = Integer.parseInt(request.getParameter("projectId"));
		Project project = projectManager.getById(projectId);
		request.setAttribute("project", project);

		// Liste complete des employes pour le projet
		List<Employee> employeeList = employeeManager
				.getListByProjectId(projectId);
		request.setAttribute("employeeList", employeeList);

		// Liste complete des activites pour le projet
		List<Activity> activityList = activityManager
				.getListByProjectId(projectId);
		request.setAttribute("activityList", activityList);

		// Liste complete des discussions pour le projet
		List<Conversation> conversationList = conversationManager
				.getListByProjectId(projectId);
		request.setAttribute("conversationList", conversationList);

		// Liste des employes avec qui l'utilisateur connecte est deja en
		// conversation pour le projet consulte
		List<Employee> interlocutorList = conversationManager
				.getInterlocutorListByProject(currentEmployee.getId(),
						projectId);
		request.setAttribute("interlocutorList", interlocutorList);

		// Liste des employes qui ne sont pas affectes au projet
		List<Employee> employeeNotInProject = employeeManager
				.getListNotInProject(projectId);
		request.setAttribute("employeeNotInProject", employeeNotInProject);

		loadJSP(SiteParameters.getPagePath("project"), request, response);
	}
}